/**
**************************************************************************************
*Palisma - Secrets of the Illuminati is an open-source 2D RPG                        *
*Copyright (C) 2006, Tony Sparks                                                     *
*                                                                                    *
*This library is free software; you can redistribute it and/or                       *
*modify it under the terms of the GNU Lesser General Public                          *
*License as published by the Free Software Foundation; either                        *
*version 2.1 of the License, or (at your option) any later version.                  *
*                                                                                    *
*This library is distributed in the hope that it will be useful,                     *
*but WITHOUT ANY WARRANTY; without even the implied warranty of                      *
*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                   *
*Lesser General Public License for more details.                                     *
*                                                                                    *
*You should have received a copy of the GNU Lesser General Public                    *
*License along with this library; if not, write to the Free Software                 *
*Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA      *
**************************************************************************************
*/
// Myriad.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "Myriad.h"

#include <stdio.h>
#include <time.h>

Kernel* g_kernel = NULL;

/**
===========================
Init each subsystem and add them to
the engine
===========================
*/
void SetupEngine()
{
    
    /**
    ======================================
    Add initial game states
    ======================================
    */
    Console* g_console = new Console();
    CMMPointer<Cvars>   cvars     = new Cvars();

    // NOTE - some what of a hack, we need to register
    // the input system before init'n the console
    // Make the Input subsystem
    CMMPointer<IInput> g_inputMngr = new Input();


    g_kernel->AddProcess( CMMPointer<IProcess> (g_inputMngr) );
    g_kernel->SetInput( g_inputMngr );
    
    // register cvars
    cvars->Init();
    g_console->Init();
    
    // The game manager init's the state
    g_console->SetName("Console");
    g_console->SetActive( true ); 
    
    // Set the Kernels subsystems
    g_kernel->SetConsole( g_console );
    g_kernel->SetCvars( cvars );

    g_console->Print( (_PROGRAM_ + _VERSION_ + "-- "+ _OS_ + "-- "+ __DATE__+ " " + " " __TIME__ ).c_str() ); 

    CMMPointer<Resource> resource = new Resource();
    g_kernel->SetResource( resource );
    // now initialize the input system
    g_inputMngr->Init();
    g_inputMngr->SetPriority( HIGH_PRIORITY+1 );
    g_inputMngr->SetKill( false );

    // Read in the Config file
    g_console->Print("Loading config.cfg file...");
    g_console->Exec("exec config.cfg");
    g_console->SetLog ( (bool)cvars->GetFloatValue("logger") );
    
    g_console->Print("Adding subsystems...");

    /*-----------------------------------------------
        Check for input type
    ------------------------------------------------*/
    if ( cvars->GetFloatValue("i_joystick") )
        g_inputMngr->EnableJoystick();

    g_console->Print("Initializing scripting engine...");
    CMMPointer<ScriptEngine> scriptEngine = new LuaEngine();
    if ( scriptEngine->Init() )
        g_console->Print("Error loading scripting engine -> %s", scriptEngine->GetImplementation().c_str() );
    else {
        g_console->Print("Scripting engine initialized!");
        g_console->Print( ("-- " + scriptEngine->GetImplementation() + " --").c_str() );
        
        g_kernel->SetScriptEngine( CMMPointer<ScriptEngine>(scriptEngine) );
    }


    	// Add the sound subsystem
	if ( cvars->GetFloatValue( "s_sound" ) )
	{
		CMMPointer<ISound> soundMng = new SoundManager;
		soundMng->Init();
		soundMng->SetPriority( MED_PRIORITY );
		soundMng->SetKill( false );

		g_kernel->AddProcess( CMMPointer<IProcess> (soundMng) );
        g_kernel->SetSound( soundMng );
        
		g_console->Print( "Sound System online..." );
	}
    // Game 
    CMMPointer<GameManager> g_gameMngr = new GameManager();
    g_gameMngr->Init();
    g_gameMngr->SetPriority( HIGH_PRIORITY );
    g_gameMngr->SetKill( false );
    
    // Now add it to the kernel
    g_kernel->AddProcess( CMMPointer<IProcess> (g_gameMngr) );
    g_console->Print("GameManager online!");
    // Make Renderer the least priority
    CMMPointer<IRenderManager> g_renderMngr = new RenderManager();
    g_renderMngr->Init();
    g_renderMngr->SetPriority( LOW_PRIORITY );
    g_renderMngr->SetKill( false );

    g_kernel->AddProcess( CMMPointer<IProcess> (g_renderMngr) );
    g_kernel->SetGame( g_gameMngr );
    g_kernel->SetRenderer( g_renderMngr );
    g_console->Print("Renderer online!");


        
    // Create the console view
    IGameStateView* con_view = new ConsoleView(g_console);
    con_view->SetZOrder( ZTOP_MOST+20 );
    con_view->SetActive( true );
    // Add the console to the game
    g_kernel->GetGame()->AddState( g_console, con_view );
    
    /*----------------------------------------------------
        Add the In Game State
    -----------------------------------------------------*/
    IGameState* ingame = new InGameState;
    ingame->SetActive( true );
    IGameStateView* inGameView = new InGameStateView;
    // Add the InGameState to the game
    inGameView->SetZOrder( ZBOTTOM_MOST );
    g_kernel->GetGame()->AddState( ingame, inGameView );
    
    /*----------------------------------------------------
        Add the Dialog state
    -----------------------------------------------------*/
    // dialog box -- TEMP make GameManager have instances of these
    IGameState* dialogState = new DialogState;
    IGameStateView* dialogView = new DialogBoxView;
    dialogView->SetZOrder( ZTOP_MOST );
    g_kernel->GetGame()->AddState( dialogState, dialogView );

    /*----------------------------------------------------
        Add the Main Menu State
    ------------------------------------------------------*/
    IGameState* menuState = new MenuState;
    menuState->Init();
    g_kernel->GetGame()->AddState( menuState, NULL );

    g_kernel->GetConsole()->Exec( ("run " + cvars->GetStringValue( "startscript" ) ).c_str() ); 

}


// Engine entry point
int _tmain(int argc, _TCHAR* argv[])
{
    g_kernel = new Kernel();
    // Init engine
    if ( !g_kernel->Init() )
    {
        /**
         * Init all engine subsystems
         */
        SetupEngine();
       
        // Run processes
        g_kernel->Execute();
    }
    // savely shutdown
    g_kernel->Shutdown();

    //clean up any remaining unreleased objects
	//IMMObject::CollectRemainingObjects(true);

    // delete the engine
    if ( g_kernel )
        delete g_kernel;
    return 0;

}

